<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DoHun Kim's Calculator</title>
</head>
<h1>Project 1A Calculator</h1>
<h3>By Do Hun Kim  (4/5/2012) </h3>
<h3> Type an Expression in the following box.</h3>

<p>
<form action="calculator.php" method = "post">
<input type = "text" name = "expr"/>
<input type = "submit" value = "Calculate"/>


</form>
</p>

<ul>
<li>Only numbers and +,-,* and / operators are allowed in the expression.
<li>The evaluation follows the standard operator precedence.
<li>The calculator does not support parentheses.

</li>

<?php
$expr = $_POST['expr'];  // Obtain Value
$expr = str_replace(' ', '', $expr);  // remove spaces


if($expr != '')   // If the expression is empty, then terminate the program
{

	

echo "<br><h3> Result  <br> ";


// Conduct some Parsing..
if(preg_match("#[^0-9.*+/-]#", $expr))  // Check for operator and numbers only.
{
	echo "<br> <h3>Invalid Input Expression (Incorrect input variables) " .$expr;
	return;
}

//Check for division by zero cases
//Ex 1, 1/0 + 1.  
if(preg_match("#/0[^.]#", $expr))  // Looks for a /0 at the beginning of the string
{
	echo "Invalid Input Expression, Can't divide by zero. " .$expr;
	return;
}

//Ex 2. 1+1/0
if(preg_match("#/0$#", $expr)) // Looks for a /0 at the end of a string
{
	echo "Invalid Input Expression, Can't divide by zero. " .$expr;
	return;
}

@eval("\$answer = $expr;");  // @ Removes PHP warnings, and evaluate 

if(isset($answer))	
	echo $expr . " = " .$answer;  // Output the answer 
else
	echo "Invalid Input Expression " .$expr;  // Error, Output error code
	
}

?>


<body>
</body>
</html>